package com.nuc.config;


import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zhang Chao
 * @Date 2021/5/11 11:13
 * @Version 1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        // 定义全局header参数
        ParameterBuilder useridPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        useridPar.name("userId").defaultValue("").description("用户id").modelRef(new ModelRef("string"))
                .parameterType("header").required(false).build();
        pars.add(useridPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).select()
                //如果不想将所有的接口都通过swagger管理的话，可以将RequestHandlerSelectors.any()修改为RequestHandlerSelectors.basePackage()
                //.apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.nuc.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);


    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 标题
                .title("中北安全")
                // 简介
                .description("参加互联网+")
                // 服务条款
                .termsOfServiceUrl("https://spring.io/")
                // 作者个人信息
                .contact("antoni.zc")
                // 版本
                .version("1.0").build();
    }
}
